package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IArticleDAO;
import org.jsola.hr.dto.ArticleAddDTO;
import org.jsola.hr.dto.ArticleUpdateDTO;
import org.jsola.hr.entity.ArticleDO;
import org.jsola.hr.query.ArticleQuery;
import org.jsola.hr.service.IArticleService;
import org.jsola.hr.vo.ArticleListVO;
import org.jsola.hr.vo.ArticleVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 人力文章
 *
 * @author zhr
 */
@Service("hrArticleServiceImpl")
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private IArticleDAO articleDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ArticleVO save(ArticleAddDTO articleAddDTO, TokenUser tokenUser) {
        // 转DO
        ArticleDO articleDO = articleAddDTO.to(ArticleDO.class);
        // 保存
        articleDO = save(articleDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增人力文章", "saveArticle", articleDO.getId() + "", tokenUser);
        return articleDO.to(ArticleVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ArticleUpdateDTO articleUpdateDTO, TokenUser tokenUser) {
        //转DO
        ArticleDO articleDO = articleUpdateDTO.to(ArticleDO.class);
        //根据主键更新，只更新非null值
        HrLogKit.logForUpdate("修改人力文章", "updateArticle", articleDO.getId() + "", tokenUser);
        return updateByIdSelective(articleDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... articleIds) {
        HrLogKit.logForDelete("批量删除人力文章", "deleteArticleByIds", Arrays.toString(articleIds), tokenUser);
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) articleIds);
    }


    @Override
    public ArticleVO selectById(Long articleId, String siteId) {
        ArticleDO articleDO = selectDOById(articleId, siteId);
        if (articleDO == null) {
            return null;
        }
        return articleDO.to(ArticleVO.class);
    }

    @Override
    public List<ArticleListVO> select(ArticleQuery articleQuery, String siteId) {
        List<ArticleDO> articleDOList = selectDO(articleQuery, siteId);
        if(CollectionUtils.isEmpty(articleDOList)) {
            return articleDOList == null ? null : new ArrayList<>();
        }
        return articleDOList.stream()
                .map(articleDO -> articleDO.to(ArticleListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ArticleQuery articleQuery, String siteId) {
        Example example = buildExample(articleQuery,siteId);
        return articleDAO.selectCountByExample(example);
    }

    @Override
    public Page<ArticleListVO> selectPage(ArticleQuery articleQuery, String siteId) {
        Example example = new Example(ArticleDO.class);
        example.and()
                .andEqualTo("valid", true);
        Page<ArticleDO> page = articleDAO.selectPageByExample(example,
                articleQuery.getPageNo(),
                articleQuery.getPageSize());

        return page.to(ArticleListVO.class);
    }

    @Override
    public ArticleDO selectDOById(Long articleId, String siteId) {
        return listById(articleId, siteId);
    }

    @Override
    public List<ArticleDO> selectDO(ArticleQuery articleQuery, String siteId) {
        Example example = buildExample(articleQuery,siteId);
        return articleDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param articleQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(ArticleQuery articleQuery, String siteId) {
        Example example = new Example(ArticleDO.class);
        example.and()
                .andEqualTo("valid", true);
        // 排序
        ExampleKit.setExampleOrder(example,articleQuery.getOrders());
        return example;
    }
}




